/**
 * 按需全局化过滤器
 * 您还可以在组件定义时的 filters 属性中按需引入
 */
// 定义方法：Vue.filter({name}, function(value){...})

import Vue from 'vue'
import store from '@/store'

Vue.filter('imageUrl', function (json) { //获取第一个文件路径
  if (json == undefined || json == null) return;
  try {
    var fileArr = JSON.parse(json);
    var path = fileArr[0].FilePath ? fileArr[0].FilePath : fileArr[0].filePath;
    return path;
  } catch (e) {
    return '';
  }
})

Vue.filter('formatPhone', function (cellValue) { //获取第一个文件路径
  if (Number(cellValue) && String(cellValue).length === 11) {
    var mobile = String(cellValue)
    var reg = /^(\d{3})\d{4}(\d{4})$/
    return mobile.replace(reg, '$1****$2')
  } else {
    return cellValue
  }
})

Vue.filter('getImageList', function (json) { //获取多个文件路径
  if (json == undefined || json == null) return;
  try {
    var fileList = JSON.parse(json);
    var fileArr = [];
    for (var i = 0; i < fileList.length; i++) {
      fileArr.push(fileList[i].filePath || fileList[i].FilePath)
    }
    return fileArr;
  } catch (e) {
    return '';
  }
})

Vue.filter('time', function (tstr, fmt) { //author: meizz
  if (!tstr) return;
  try {
    tstr = tstr.replace('T', ' ').replace('Z', '')
  } catch (err) { }
  let date = new Date(tstr);
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }

  function padLeftZero (str) {
    return ('00' + str).substr(str.length)
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
    }
  }
  return fmt

})

//金额格式化
Vue.filter('pointNum', function (obj) {
  obj = obj.replace(/[^-\d.]/g, ""); //清除"数字"和"."以外的字符
  obj = obj.replace(/^\./g, ""); //验证第一个字符是数字
  obj = obj.replace(/^\--/, "-"); //以0开头的不允许出现两个0
  obj = obj.replace(/^00/, "0"); //以0开头的不允许出现两个0
  obj = obj.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
  obj = obj.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
  obj = obj.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
  return obj;
})
// Vue.filter('flatten',function(arr) {
//   return [].concat( ...arr.map(x => Array.isArray(x.items) ? Vue.filter('flatten')(x.items) : x) )
// })

Vue.directive('permission', {
  bind (el, binding, vNode) {
    if (binding.value) {
      el.style.display = 'none'
      if (global.gApp.$store.state.menu.operations.some(ele => ele == binding.value)) {
        el.style.display = binding.modifiers.block ? 'block' : 'inline-block'
      }
    }
  }
})


Vue.filter('amountTransform', function (number) {
  let ret = ''
  if (number !== '' && number != null && number !== '0') {
    let unit = '仟佰拾亿仟佰拾万仟佰拾元角分'
    let str = ''
    number += '00'
    const point = number.indexOf('.')
    if (point >= 0) {
      number = number.substring(0, point) + number.substr(point + 1, 2)
    }
    unit = unit.substr(unit.length - number.length)
    for (let i = 0; i < number.length; i++) {
      str +=
        '零壹贰叁肆伍陆柒捌玖'.charAt(number.charAt(i)) + unit.charAt(i)
    }
    ret =
      str
        .replace(/零(仟|佰|拾|角)/g, '零')
        .replace(/(零)+/g, '零')
        .replace(/零(万|亿|元)/g, '$1')
        .replace(/(亿)万|(拾)/g, '$1$2')
        .replace(/^元零?|零分/g, '')
        .replace(/元$/g, '元') + '整'
  }
  return ret
})

//成件数量
Vue.filter('pieceCount', function (count, singleCount) {
  return Math.floor(Number(count) / Number(singleCount))
})
